package IBBB.view;

import IBBB.model.UserMessage;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.text.Text;
import IBBB.network.sendmessage.send_changeplan;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class NewPlanBox {

    @FXML
    private Button newPlanButton;

    @FXML
    private Text newPlanTitle;

    @FXML
    private Text newPlanWords;

    @FXML
    private Text newPlanNeedDays;

    @FXML
    private ChoiceBox newPlanDayWord;

    @FXML
    private ImageView wordStockImage;

    private UserMessage userMessage;

    private void warn(String s, String t) {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("WarningPane.fxml"));
        try {
            Parent parent = loader.load();
            WarningPane wp = (WarningPane)loader.getController();
            wp.setMessage(s);
            Stage w = new Stage(StageStyle.UTILITY);
            w.setTitle(t);
            w.setScene(new Scene(parent, 350,220));
            w.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @FXML
    void setNewPlanButton(ActionEvent event) {
        newPlanButton.setDisable(true);
        send_changeplan changeplan = new send_changeplan("localhost", userMessage.getUserName().get(), newPlanTitle.getText(), (int) newPlanDayWord.getValue());
        changeplan.messageProperty().addListener(((observableValue, s, t1) -> {
            switch (t1) {
                case "timeout": {
                    warn("网络异常", "警告");
                    newPlanButton.setDisable(false);
                    break;
                }
                case "wrong": {
                    warn("出现异常，新建失败", "警告");
                    newPlanButton.setDisable(false);
                    break;
                }
                case "success": {
                    userMessage.addSchedule(newPlanTitle.getText(), (int) newPlanDayWord.getValue());
                    newPlanButton.setDisable(false);
                    break;
                }
            }
        }));
        Thread t = new Thread(changeplan);
        t.start();
    }

    @FXML
    private void initialize() {
        newPlanDayWord.setItems(FXCollections.observableArrayList(5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60));
        newPlanDayWord.getSelectionModel().selectedItemProperty().addListener((observableValue, o, t1) -> {
            Integer totalWords=Integer.parseInt(newPlanWords.getText());
            Integer dailyWords=(Integer)newPlanDayWord.getValue();
            Integer needDays=totalWords/dailyWords;
            newPlanNeedDays.setText(String.valueOf(needDays));
    });
    }

    public void setMessage(UserMessage message, String name,Integer totalNum,boolean planned) {
        userMessage=message;
        newPlanTitle.setText(name);
        newPlanWords.setText(String.valueOf(totalNum));
        if(planned){
            newPlanDayWord.setValue(userMessage.getPlan(newPlanTitle.getText()));
            newPlanButton.setText("已有计划");
            newPlanButton.setDisable(true);
        } else {
            newPlanDayWord.setValue(5);
        }
        wordStockImage.setImage(new Image(newPlanTitle.getText() + ".png"));
    }
}
